<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="个人博客">
<meta property="og:url" content="http://codercheng.cn/page/4/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="CoderCheng">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="个人博客" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.lug.ustc.edu.cn/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://codercheng.cn"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="post-Linux 一些命令" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/12/16/Linux%20%E4%B8%80%E4%BA%9B%E5%91%BD%E4%BB%A4/" class="article-date">
  <time datetime="2020-12-16T10:00:12.268Z" itemprop="datePublished">2020-12-16</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/12/16/Linux%20%E4%B8%80%E4%BA%9B%E5%91%BD%E4%BB%A4/">Linux 一些命令</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <hr>
<h2 id="categories-linux"><a href="#categories-linux" class="headerlink" title="categories: linux"></a>categories: linux</h2><p>Linux 一些命令</p>
<p>jar 重命名</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rename jar jar.back xx.jar</span><br></pre></td></tr></table></figure>



<p>查看防火墙状态</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl <span class="built_in">status</span>  firewalld</span><br></pre></td></tr></table></figure>



<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">service firewalld stop</span><br></pre></td></tr></table></figure>



<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --permanent --add-port&#x3D;3306&#x2F;tcp</span><br></pre></td></tr></table></figure>




<p># 查询端口是否开放</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --query-port&#x3D;3306&#x2F;tcp</span><br></pre></td></tr></table></figure>

<p>#重启防火墙(修改配置后要重启防火墙)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>



<p> 查看centos版本</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cat /etc/centos-release</span><br></pre></td></tr></table></figure>

<p>该目录是系统是可执行二进制文件的默认目录    <strong>/usr/local/bin/</strong></p>
<p>如果需要命令将直接在控制台使用  可以 建立个软链接</p>
<p> 如下 ：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">update user set host &#x3D; &#39;%&#39; where user &#x3D; &#39;root&#39;;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">flush privileges;</span><br></pre></td></tr></table></figure>

<p>masterha_check_ssh -conf=/etc/mha_master/mha.cnf</p>
<p>masterha_check_repl –conf==/etc/mha_master/mha.cnf</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen</span><br><span class="line">cd ~</span><br><span class="line">cd .ssh&#x2F;</span><br><span class="line">cat id_rsa.pub</span><br><span class="line"></span><br><span class="line">ssh-copy-id username@host</span><br></pre></td></tr></table></figure>





<p> 查看 java 进程 占用的端口</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">netstat -anp|grep java</span><br></pre></td></tr></table></figure>



<p>change  master to master_host=’39.106.214.114’,master_port=3306,master_user=’root’,master_password=’root’,master_log_file=’mysql-bin.000014’,master_log_pos=154;</p>
<p>start slave;<br>STOP SLAVE;<br>show  slave status</p>
<p>INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so’;</p>
<p>show VARIABLES LIKE ‘%semi%’</p>
<p>set GLOBAL  rpl_semi_sync_slave_enabled=1</p>
<p>change  master to master_host=’39.106.214.114’,master_port=3306,master_user=’root’,master_password=’root’,master_log_file=’mysql-bin.000016’,master_log_pos=759;</p>
<p>start slave;<br>STOP SLAVE;</p>
<p>slave reset;</p>
<p>INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so’;</p>
<p>show VARIABLES LIKE ‘%semi%’</p>
<p>set GLOBAL  rpl_semi_sync_slave_enabled=1</p>
<p>//批量替换文件中的内容<br>sed -i “s/查找字段/替换字段/g” grep 查找字段 -rl 路径 文件名<br>示例：sed -i “s/shan/hua/g” lishan.txt<br>把当前目录下lishan.txt里的shan都替换为hua</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/12/16/Linux%20%E4%B8%80%E4%BA%9B%E5%91%BD%E4%BB%A4/" data-id="cko3snvwf0007i8uo2hk9959x" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Spring Cloud Alibaba 学习笔记" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/12/04/Spring%20Cloud%20Alibaba%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2020-12-04T02:43:20.758Z" itemprop="datePublished">2020-12-04</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/SpringCloud/">SpringCloud</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/12/04/Spring%20Cloud%20Alibaba%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">Spring Cloud Alibaba 学习笔记</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>​                                                        Spring Cloud Alibaba 学习笔记</p>
<p>第⼀代 Spring Cloud （主要是 SCN）很多组件已经进⼊停更维护模式。</p>
<p>Spring Cloud：Netflflix，Spring官⽅，SCA（被Spring官⽅认可）</p>
<p>注意：市场上主要使⽤的还是SCN，SCA⼀套框架的集合。</p>
<p>Alibaba 更进⼀步，搞出了Spring Cloud Alibaba（SCA），SCA 是由⼀些阿⾥巴巴的开源组件和云产品组成的，2018年，Spring Cloud Alibaba 正式⼊住了 SpringCloud 官⽅孵化器。</p>
<p><strong>各个组件</strong></p>
<p><strong>Nacos（服务注册中⼼、配置中⼼）</strong></p>
<p><strong>Sentinel哨兵（服务的熔断、限流等）</strong></p>
<p><strong>Dubbo RPC/LB</strong></p>
<p><strong>Seata分布式事务解决⽅案</strong></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/12/04/Spring%20Cloud%20Alibaba%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-id="cko3snvwu000ii8uohd89ehjp" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-SpringCloudAlibaba 编程题" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/12/04/SpringCloudAlibaba%20%E7%BC%96%E7%A8%8B%E9%A2%98/" class="article-date">
  <time datetime="2020-12-04T02:32:23.497Z" itemprop="datePublished">2020-12-04</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E4%BD%9C%E4%B8%9A/">作业</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/12/04/SpringCloudAlibaba%20%E7%BC%96%E7%A8%8B%E9%A2%98/">SpringCloudAlibaba 编程题</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <pre><code>##  SpringCloudAlibaba 编程题 ## </code></pre><p>1 、提供资料：代码工程、验证及讲解视频。</p>
<p>2、讲解内容包含：题目分析、实现思路、代码讲解。</p>
<p>3、效果展示：</p>
<p>1)Eureka注册中心 替换为 Nacos注册中心</p>
<p>2)Config+Bus配置中心 替换为 Nacos配置中心</p>
<p>3)Feign调用 替换为 Dubbo RPC调用</p>
<p>4)使用Sentinel对GateWay网关的入口资源进行限流（限流参数自定义并完成测试即可） </p>
<p>作业效果同springcloud1</p>
<p>​     接下来 展示效果</p>
<ol>
<li><p>先看Nacos 注册的服务列表</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/110118-553494.png" alt="image-20201204100918828"></p>
</li>
</ol>
<p>说明服务已经注册成功。</p>
<ol start="2">
<li>在看Nacos 中 GateWay 的配置信息</li>
</ol>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/101224-25191.png" alt="image-20201204101223335"></p>
<p>GateWay的成功启动说明已经从Nacos中拿到配置信息了。</p>
<ol start="3">
<li>接下来显示页面操作</li>
</ol>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/101614-915981.png" alt="image-20201204101613688"></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/101627-554344.png" alt="image-20201204101627304"></p>
<ol start="4">
<li>在Sentinel Dashboard  中配置流控规则 如下图   说明 （配置Gateway 和具体服务的菜单会有不同 ，网关会显示下面箭头的部分）</li>
</ol>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/101825-363667.png" alt="image-20201204101825430"></p>
<p>​        </p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/102045-126699.png" alt="image-20201204102044478"></p>
<p>可以配置两种类型的api  一种是整个服务  一种对接口</p>
<p>我们对已api/user 为前缀的接口进行流控</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/102404-718014.png" alt="image-20201204102404149"></p>
<p> 这个页面会调用 api/user/info 这个接口    在没有添加流控规则多次刷新页面正常拿到数据。</p>
<ol start="5">
<li>添加流控规则</li>
</ol>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/102628-21927.png" alt="image-20201204102627667"></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/103152-867818.png" alt="image-20201204102708740"></p>
<p> 说明  每秒超过1个QPS 会进行60s的快速失败。</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202012/04/102838-150842.png" alt="image-20201204102838188"></p>
<p>   页面显示限流信息 成功。</p>
<p><strong>整体思路</strong> ： 根据需要依赖进行整合SpringCloudAlibaba 。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/12/04/SpringCloudAlibaba%20%E7%BC%96%E7%A8%8B%E9%A2%98/" data-id="cko3snvwv000ki8uo3xxj8uit" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Spring Cloud 学习笔记" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/11/27/Spring%20Cloud%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="article-date">
  <time datetime="2020-11-27T08:33:03.505Z" itemprop="datePublished">2020-11-27</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/SpringCloud/">SpringCloud</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/11/27/Spring%20Cloud%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">Spring Cloud 学习笔记</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="Spring-Cloud-学习笔记"><a href="#Spring-Cloud-学习笔记" class="headerlink" title="Spring Cloud 学习笔记"></a>Spring Cloud 学习笔记</h3><h2 id="微服务架构"><a href="#微服务架构" class="headerlink" title="微服务架构"></a>微服务架构</h2><h2 id="SpringCloud-简称-SCN"><a href="#SpringCloud-简称-SCN" class="headerlink" title="SpringCloud 简称 SCN"></a>SpringCloud 简称 SCN</h2><p> 各个组件说明</p>
<ul>
<li><p>1   Eureka   注册中心</p>
</li>
<li><p>2   Ribbon    负载均衡</p>
</li>
<li><p>3    Hystrix   熔断保护</p>
</li>
<li><p>4     Feign     远程调用</p>
</li>
<li><p>5     GateWay    网关  </p>
</li>
<li><p>6    Config     配置中心</p>
</li>
<li><p>7     Stream    消息驱动组件</p>
<p><strong>总结</strong>：各个组件组成一个体系 构成了SpringCloud 体系。</p>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/11/27/Spring%20Cloud%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" data-id="cko3snvwr000ei8uof98z6fwi" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-SpringCloud 作业编程题" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/11/27/SpringCloud%20%E4%BD%9C%E4%B8%9A%E7%BC%96%E7%A8%8B%E9%A2%98/" class="article-date">
  <time datetime="2020-11-27T08:18:37.287Z" itemprop="datePublished">2020-11-27</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E4%BD%9C%E4%B8%9A/">作业</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/11/27/SpringCloud%20%E4%BD%9C%E4%B8%9A%E7%BC%96%E7%A8%8B%E9%A2%98/">SpringCloud 作业编程题</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>​                                                SpringCloud 作业编程题</p>
<p><strong>整体架构如下图：</strong></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/155606-993025.png" alt></p>
<p>实现效果如下：</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/164355-872457.png" alt></p>
<p>  接下来（<strong>Show Time</strong>）</p>
<p>效果如下</p>
<p>首先先准备配置项 </p>
<ul>
<li>1  将 本地Host  <a href="http://www.test.com" target="_blank" rel="noopener">www.test.com</a>  指向本地  (说明 防止静态页面访问跨域)</li>
<li>2   启动需要的项目 效果如下：</li>
</ul>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/160521-160445.png" alt="image-20201127160519706"></p>
<ul>
<li>3   接下来  访问静态页面 测试 </li>
</ul>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/160758-257175.png" alt="image-20201127160756723"></p>
<p>  <strong>当输入不存在邮箱时 弹窗提示</strong></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/160911-615933.png" alt="image-20201127160910249"></p>
<p>​        <strong>进入注册页面</strong></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/161129-117372.png" alt="image-20201127161128142"></p>
<p> <strong>收到邮箱验证码</strong></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/164357-974758.png" alt="image-20201127161455123"></p>
<p> <strong>输入错误的验证码</strong></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/161524-696498.png" alt="image-20201127161522896"></p>
<p>输入正确的验证码 提示并跳转登录界面</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/161557-364667.png" alt="image-20201127161557408"></p>
<p>登录界面输入错误的密码时</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/161728-146800.png" alt="image-20201127161728051"></p>
<p>登录界面输入正确的密码时</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/161815-714463.png" alt="image-20201127161815852"></p>
<p> 接下来 看看 ip暴力测试</p>
<p>​      码云的配置1分钟 限制3次</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/164411-479111.png" alt="image-20201127162115878"></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/27/162126-617649.png" alt="image-20201127162126670"></p>
<p> 页面显示 请求拒绝    成功！</p>
<p> <strong>整体思路： 其实是在原来基础上进行增加 对应的操作 实现逻辑。</strong></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/11/27/SpringCloud%20%E4%BD%9C%E4%B8%9A%E7%BC%96%E7%A8%8B%E9%A2%98/" data-id="cko3snvwz000ri8uoeae50yvo" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Dubbo介绍" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/11/11/Dubbo%E4%BB%8B%E7%BB%8D/" class="article-date">
  <time datetime="2020-11-11T09:50:21.408Z" itemprop="datePublished">2020-11-11</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E6%A1%86%E6%9E%B6/">框架</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/11/11/Dubbo%E4%BB%8B%E7%BB%8D/">Dubbo介绍</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <pre><code>Dubbo 学习笔记</code></pre><h3 id="Dubbo介绍"><a href="#Dubbo介绍" class="headerlink" title="Dubbo介绍"></a>Dubbo介绍</h3><p><a href="http://dubbo.apache.org/zh-cn/docs/2.7/user/preface/background/" target="_blank" rel="noopener">官网参考资料</a></p>
<h2 id="什么是Dubbo"><a href="#什么是Dubbo" class="headerlink" title="什么是Dubbo"></a>什么是Dubbo</h2><p>Apache Dubbo是 一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架，可以和Spring框架无缝集成。</p>
<h3 id="Dubbo特性"><a href="#Dubbo特性" class="headerlink" title="Dubbo特性"></a>Dubbo特性</h3><ul>
<li><p>面向接口代理的高性能RPC调用</p>
</li>
<li><p>智能负载均衡</p>
</li>
<li><p>服务自动注册和发现</p>
</li>
<li><p>高度可扩展能力 SPI</p>
</li>
<li><p>可视化服务治理与运维</p>
<h3 id="Dubbo处理流程"><a href="#Dubbo处理流程" class="headerlink" title="Dubbo处理流程"></a>Dubbo处理流程</h3></li>
</ul>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/11/173721-660328.png" alt="下载"></p>
<p>图解：</p>
<table>
<thead>
<tr>
<th>节点</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>Provider</td>
<td>服务提供者</td>
</tr>
<tr>
<td>Consumer</td>
<td>服务消费者</td>
</tr>
<tr>
<td>Registry</td>
<td>服务注册与发现的注册中心</td>
</tr>
<tr>
<td>Monitor</td>
<td>监控中心，统计服务调用次数调用时间</td>
</tr>
<tr>
<td>Container</td>
<td>服务运行容器，负责启动加载运行生产者</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th>线条</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>蓝色虚线</td>
<td>服务启动后的初始化过程</td>
</tr>
<tr>
<td>红色虚线</td>
<td>异步过程</td>
</tr>
<tr>
<td>红色实现</td>
<td>同步调用过程</td>
</tr>
</tbody></table>
<h2 id="Dubbo高级进阶"><a href="#Dubbo高级进阶" class="headerlink" title="Dubbo高级进阶"></a>Dubbo高级进阶</h2><h3 id="扩展点配置-SPI"><a href="#扩展点配置-SPI" class="headerlink" title="扩展点配置 SPI"></a>扩展点配置 SPI</h3><p>SPI 全称为 (Service Provider Interface) ，是JDK内置的一种服务提供发现机制。</p>
<p>目前有不少框架用它来做服务的扩展发现，简单来说，它就是一种动态替换发现的机制。</p>
<p>使用SPI机制的优势是实现解耦，使得第三方服务模块的装配控制逻辑与调用者的业务代码分离。</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/30/170852-510525.png" alt="JDK-SPI"></p>
<p>Java中如果想要使用SPI功能，先提供标准服务接口，然后再提供相关接口实现和调用者。这样就可以通过SPI机制中约定好的信息进行查询相应的接口实现。</p>
<p>SPI遵循如下约定:</p>
<ol>
<li><p>当服务提供者提供了接口的一种具体实现后，在META-INF/services目录下创建一个以“接口全限定名”为命名的文件，内容为实现类的全限定名;</p>
</li>
<li><p>接口实现类所在的jar包放在主程序的classpath中;</p>
</li>
<li><p>主程序通过java.util.ServiceLoader动态装载实现模块。</p>
<p>它通过扫描META-INF/services目录下的配置文件找到实现类的全限定名，把类加载到JVM;</p>
</li>
<li><p>SPI的实现类必须携带一个无参构造方法</p>
</li>
</ol>
<h4 id="扩展点-Dubbo中的SPI"><a href="#扩展点-Dubbo中的SPI" class="headerlink" title="扩展点-Dubbo中的SPI"></a>扩展点-Dubbo中的SPI</h4><h5 id="来源"><a href="#来源" class="headerlink" title="来源"></a>来源</h5><p>Dubbo 的扩展点加载从 JDK 标准的 SPI (Service Provider Interface) 扩展点发现机制加强而来。</p>
<p>Dubbo 改进了 JDK 标准的 SPI 的以下问题：</p>
<ul>
<li>JDK 标准的 SPI 会<strong>一次性实例化扩展点所有实现</strong>，如果有扩展实现初始化很耗时，但如果没用上也加载，会很浪费资源。</li>
<li>如果扩展点加载失败，连扩展点的名称都拿不到了。比如：JDK 标准的 ScriptEngine，通过<code>getName()</code> 获取脚本类型的名称，但如果 RubyScriptEngine 因为所依赖的 jruby.jar 不存在，导致 RubyScriptEngine 类加载失败，这个失败原因被吃掉了，和 ruby 对应不起来，当用户执行 ruby 脚本时，会报不支持 ruby，而不是真正失败的原因。</li>
<li>增加了对扩展点 IoC 和 AOP 的支持，一个扩展点可以直接 setter 注入其它扩展点。</li>
</ul>
<h5 id="约定"><a href="#约定" class="headerlink" title="约定"></a>约定</h5><ol>
<li>当服务提供者提供了接口的一种具体实现后，在`META-INF/dubbo/目录下创建一个以“接口全限定名”为命名的文件，内容为实现类的全限定名</li>
<li>内容为：<code>配置名=扩展实现类全限定名</code>，多个实现类用换行符分隔</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/11/11/Dubbo%E4%BB%8B%E7%BB%8D/" data-id="cko3snvwb0005i8uo4t3vdp93" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Dubbo习题说明" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/11/11/Dubbo%E4%B9%A0%E9%A2%98%E8%AF%B4%E6%98%8E/" class="article-date">
  <time datetime="2020-11-11T09:34:57.649Z" itemprop="datePublished">2020-11-11</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/11/11/Dubbo%E4%B9%A0%E9%A2%98%E8%AF%B4%E6%98%8E/">Dubbo习题说明</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <hr>
<h2 id="categories-习题"><a href="#categories-习题" class="headerlink" title="categories: 习题"></a>categories: 习题</h2><pre><code>Dubbo编程题      </code></pre><h3 id="项目结构说明"><a href="#项目结构说明" class="headerlink" title="项目结构说明"></a>项目结构说明</h3><ul>
<li>dubbo-web      测试Consumer</li>
<li>dubbo-server-monitor   TP90  TP99性能监听Filter</li>
<li>dubbo_spi_filter   IP透传Filter</li>
<li>service-api    通用RPC接口</li>
<li>service-provider           服务端</li>
<li>service-provider-1       服务端1号</li>
</ul>
<h3 id="编程题一：将Web请求IP透传到Dubbo服务中"><a href="#编程题一：将Web请求IP透传到Dubbo服务中" class="headerlink" title="编程题一：将Web请求IP透传到Dubbo服务中"></a>编程题一：将Web请求IP透传到Dubbo服务中</h3><p> 先看 实现效果  上图  </p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/11/171252-508620.png" alt="image-20201111171252402"></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/11/171316-28018.png" alt="image-20201111171314121"></p>
<p> <img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/11/171344-943669.png" alt="image-20201111171343847">   实现步骤：</p>
<p>1、service-api 中定义RPC测试接口 HelloService#sayHello方法</p>
<p>2、采用不同配置端口实现启动两个Dubbo服务端</p>
<p>3、dubbo_spi_filter  利用dubbo的spi机制定义Filter，从RPCContext中读取Consumer中传递的IP，两个Dubbo服务端皆依赖该模块 TransportIPFilter</p>
<p>服务端</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Result <span class="title">invoke</span><span class="params">(Invoker&lt;?&gt; invoker, Invocation invocation)</span> <span class="keyword">throws</span> RpcException </span>&#123;</span><br><span class="line">         String ip = RpcContext.getContext().getAttachment(<span class="string">"IP"</span>);</span><br><span class="line">         <span class="keyword">if</span>(StringUtils.isNoneBlank(ip))&#123;</span><br><span class="line">             log.info(<span class="string">"当前请求IP：&#123;&#125;"</span>,ip);</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="comment">// 执行方法</span></span><br><span class="line">         <span class="keyword">return</span>  invoker.invoke(invocation);</span><br><span class="line"></span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p>客户端<img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/11/171519-819862.png" alt="image-20201111171512526"></p>
<h3 id="编程题二：简易版Dubbo方法级性能监控"><a href="#编程题二：简易版Dubbo方法级性能监控" class="headerlink" title="编程题二：简易版Dubbo方法级性能监控"></a>编程题二：简易版Dubbo方法级性能监控</h3><h4 id="测试结果-（5秒钟打印一次记录）"><a href="#测试结果-（5秒钟打印一次记录）" class="headerlink" title="测试结果 （5秒钟打印一次记录）"></a>测试结果 （5秒钟打印一次记录）</h4><p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/11/172955-469131.png" alt="image-20201111172954789"></p>
<h4 id="实现步骤"><a href="#实现步骤" class="headerlink" title="实现步骤"></a>实现步骤</h4><p>1、 server-api定义了RPC接口，接口中定义了三个方法</p>
<ul>
<li>HelloService#sayHello</li>
<li>HelloService#sayHello1</li>
<li>HelloService#sayHello2</li>
</ul>
<p>2 、 server-provider 中提供Dubbo服务实现api中定义的HelloService接口</p>
<p>3 、 dubbo-server-monitor中定义Filter拦截RPC请求并将请求时间（KEY）和请求耗时（VALUE）存储到Map中 核心代码： TPMonitor</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Slf</span>4j</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TPMonitor</span> <span class="keyword">implements</span>  <span class="title">Runnable</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> VALID_TIME = <span class="number">1000</span>*<span class="number">60</span>;</span><br><span class="line">    <span class="keyword">private</span> Map&lt;Long, Long&gt; useTimeMaps= <span class="keyword">new</span> ConcurrentHashMap&lt;&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 对外暴露调用接口存储耗时</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">request</span><span class="params">(Long requestTime,Long useTime)</span></span>&#123;</span><br><span class="line">        useTimeMaps.put(requestTime,useTime);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 清除过期数据</span></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">clearOld</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">long</span> valid = System.currentTimeMillis()-VALID_TIME;</span><br><span class="line">        Set&lt;Long&gt; requestTimes = useTimeMaps.keySet();</span><br><span class="line">        <span class="keyword">for</span> (Long r:requestTimes)&#123;</span><br><span class="line">            <span class="keyword">if</span>(r&lt;valid)&#123;</span><br><span class="line">                useTimeMaps.remove(r);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">while</span> (<span class="keyword">true</span>)&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Thread.sleep(<span class="number">5000</span>);</span><br><span class="line">                <span class="comment">// 执行前先清除过期数据</span></span><br><span class="line">                 clearOld();</span><br><span class="line">                <span class="keyword">final</span> Map&lt;Long,Long&gt; temp = Maps.newHashMap(useTimeMaps);</span><br><span class="line">                <span class="keyword">int</span> total = temp.size();</span><br><span class="line">                <span class="keyword">if</span> (total == <span class="number">0</span>)&#123;</span><br><span class="line">                    <span class="keyword">continue</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">int</span> tp90 = (<span class="keyword">int</span>) (total*<span class="number">0.9</span>);</span><br><span class="line">                <span class="keyword">int</span> tp99 = (<span class="keyword">int</span>) (total*<span class="number">0.99</span>);</span><br><span class="line">                <span class="keyword">final</span> Collection&lt;Long&gt; requestTimes = temp.values();</span><br><span class="line"></span><br><span class="line">                Object[] sortedRequestTimes = requestTimes.stream().sorted().toArray();</span><br><span class="line">                log.info(<span class="string">"=====&gt;&gt;&gt;&gt;&gt; tp90:&#123;&#125;ms"</span>,sortedRequestTimes[tp90]);</span><br><span class="line">                log.info(<span class="string">"=====&gt;&gt;&gt;&gt;&gt; tp99:&#123;&#125;ms"</span>,sortedRequestTimes[tp99]);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>TPMonitorFilter</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">private</span>  <span class="keyword">final</span> TPMonitor tpMonitor = <span class="keyword">new</span> TPMonitor();</span><br><span class="line"></span><br><span class="line"><span class="comment">//注意此构造必须是公有的 不然 拦截器会失效</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="title">TPMonitorFilter</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    ExecutorService executorService = Executors.newFixedThreadPool(<span class="number">1</span>);</span><br><span class="line">    executorService.submit(tpMonitor);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Result <span class="title">invoke</span><span class="params">(Invoker&lt;?&gt; invoker, Invocation invocation)</span> <span class="keyword">throws</span> RpcException </span>&#123;</span><br><span class="line">    log.info(<span class="string">"请求过来了----------"</span>);</span><br><span class="line">    <span class="keyword">long</span> start = System.currentTimeMillis();</span><br><span class="line">    Result result = invoker.invoke(invocation);</span><br><span class="line">    <span class="keyword">long</span> useTime = System.currentTimeMillis() - start;</span><br><span class="line">    tpMonitor.request(System.currentTimeMillis(), useTime);</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/11/11/Dubbo%E4%B9%A0%E9%A2%98%E8%AF%B4%E6%98%8E/" data-id="cko3snvvz0000i8uo3v2125e0" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-Zookeeper" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/10/26/Zookeeper/" class="article-date">
  <time datetime="2020-10-26T06:07:12.612Z" itemprop="datePublished">2020-10-26</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Zookeeper/">Zookeeper</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/10/26/Zookeeper/">Zookeeper</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>Zookeeper </p>
<p>1 定义</p>
<p>它是一个分布式服务框架，是Apache Hadoop 的一个子项目，它主要是用来解决分布式应用中经常遇到的一些数据管理问题，如：统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等。</p>
<p>2 结构</p>
<p><strong>1、文件系统</strong></p>
<p>Zookeeper维护一个类似文件系统的数据结构：</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/26/175239-655046.png" alt="201807121434154"></p>
<p>每个子目录项如 NameService 都被称作为 znode(目录节点)，和文件系统一样，我们能够自由的增加、删除znode，在一个znode下增加、删除子znode，唯一的不同在于znode是可以存储数据的</p>
<ul>
<li><p><strong>PERSISTENT-持久化目录节点</strong></p>
<p>客户端与zookeeper断开连接后，该节点依旧存在</p>
</li>
<li><p><strong>PERSISTENT_SEQUENTIAL-持久化顺序编号目录节点</strong></p>
<p>客户端与zookeeper断开连接后，该节点依旧存在，只是Zookeeper给该节点名称进行顺序编号</p>
</li>
<li><p><strong>EPHEMERAL-临时目录节点</strong></p>
<p>客户端与zookeeper断开连接后，该节点被删除</p>
</li>
<li><p><strong>EPHEMERAL_SEQUENTIAL-临时顺序编号目录节点</strong></p>
<p>客户端与zookeeper断开连接后，该节点被删除，只是Zookeeper给该节点名称进行顺序编号</p>
</li>
</ul>
<p><strong>2、 监听通知机制</strong></p>
<p>客户端注册监听它关心的目录节点，当目录节点发生变化（数据改变、被删除、子目录节点增加删除）时，zookeeper会通知客户端。</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202011/30/171210-359254.png" alt="img"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/10/26/Zookeeper/" data-id="cko3snvx2000xi8uoe0lefcjs" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-基于netty 实现自定义RPC" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/10/19/%E5%9F%BA%E4%BA%8Enetty%20%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89RPC/" class="article-date">
  <time datetime="2020-10-19T04:04:34.963Z" itemprop="datePublished">2020-10-19</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/Netty/">Netty</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/10/19/%E5%9F%BA%E4%BA%8Enetty%20%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89RPC/">基于netty 实现自定义RPC</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>​     基于netty 实现自定义RPC</p>
<p>1.ConsumerBoot类： 启动客户端，并调用ConsumerBoot.createProxy()方法 </p>
<p>2.RPCConsumer类： 初始化客户端配置，并加入UserClientHandler客户端处理器，并生成代理对象执行sayHello()方法，即发送请求， 实际是交给UserClientHandler处理，主要有以下几步： （1）执行channelActive()方法，创建事件处理器上下文对象； （2）执行setParam()方法，设置请求对象，作为传递参数； （3）执行call()方法，将客户端数据写到服务端； …服务端处理… （4）执行channelRead()，获得服务端返回数据；</p>
<p> 3.RpcProviderApplication类： 启动服务端，并把需要的组件加载到spring容器中，初始化调用NettyServer.startServer()方法</p>
<p> 4.NettyServer类： 初始化服务端配置，并加入UserServiceHandler服务端处理器，当监听到客户端有数据写入时， UserServiceHandler服务端处理器进行处理，主要有以下几步： （1）执行channelRead()方法，这里使用CGLIB动态代理生成服务端代理对象，并执行请求对象中要执行的方法， 实际执行的是由UserServiceImpl实现类处理； （2）UserServiceImpl实现类执行sayHello()方法，返回”success”；</p>
<p>效果如下</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/19/133640-713348.png" alt="image-20201019133638037"></p>
<p>关键过程：（服务端）</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/19/134022-937307.png" alt="image-20201019134021209"></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/19/134151-217310.png" alt="image-20201019134137663"></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/19/134156-947882.png" alt="image-20201019134154823"></p>
<p>（客户端）</p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/19/134238-149549.png" alt="image-20201019134238399"></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/19/134300-419178.png" alt="image-20201019134259294"></p>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/19/134323-158233.png" alt="image-20201019134323332"></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/10/19/%E5%9F%BA%E4%BA%8Enetty%20%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%AE%9A%E4%B9%89RPC/" data-id="cko3snvxf001oi8uo5m39cis8" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  
    <article id="post-分布式调度" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2020/10/10/%E5%88%86%E5%B8%83%E5%BC%8F%E8%B0%83%E5%BA%A6/" class="article-date">
  <time datetime="2020-10-10T06:09:05.173Z" itemprop="datePublished">2020-10-10</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E8%B0%83%E5%BA%A6/">分布式调度</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/2020/10/10/%E5%88%86%E5%B8%83%E5%BC%8F%E8%B0%83%E5%BA%A6/">分布式调度</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="分布式调度"><a href="#分布式调度" class="headerlink" title="分布式调度"></a>分布式调度</h3><p>含义：</p>
<ol>
<li>运行在分布式集群环境下的调度任务（同一个定时任务程序部署多份，只应该有一个定时任务在执行）</li>
<li>分布式调度一&gt;定时任务的分布式一&gt;定时任务的拆分（即为把一个大的作业任务拆分为多个小的作<br> 任务，同时执行)</li>
</ol>
<p><img src="https://gitee.com/adc123321/blog_img/raw/master/image/202010/10/142035-855334.png" alt="image-20201010142035844"></p>
<p>定时任务和消息队列的区别</p>
<ol>
<li><p>共同点：</p>
<p>异步处理<br>比如注册、下单事件<br>应用解耦<br>不管定时任务作业还是MQ都可以作为两个应用之间的齿轮实现应用解耦，这个齿轮可以中转<br>数据，当然单体服务不需要考虑这些，服务拆分的时候往往都会考虑<br>流量削峰<br>双十一的时候，任务作业和MQ都可以用来扛流量，后端系统根据服务能力定时处理订单或者<br>从MQ抓取订单抓取到一个订单到来事件的话触发处理，对于前端用户来说看到的结果是已经<br>单成功了，下单是不受任何影响的。</p>
</li>
<li><p>本质不同<br> 定时任务作业是时间驱动，而MQ是事件驱动<br> 时间驱动是不可代替的，比如金融系统每日的利息结算，不是说利息来一条（利息到来事件）就算<br> 而往往是通过定时任务批量计算<br> 所以，定时任务作业更倾向于批处理，MQ傾向于逐条处理</p>
</li>
</ol>
<p>分布式调度框架 Elastic-Job</p>
<p><strong>Elastic-Job是当当网在2015年开源的一个分布式调度解决方案，由两个相互独立的子项目Elastic-Job-Lite和Elastic-Job-Cloud组成。在这里，主要对Elastic-Job-Lite技术做分析</strong></p>
<p><img src="https://sdkfiledl.jiguang.cn/community/30070/image/Fth9FI4AGb7sTTp-guuGbsRCsf3H.png" alt="null"></p>
<p>如上图所示，elastic-job-lite通过zk实现各服务的注册、控制及协调：</p>
<ul>
<li>第一台服务器上线触发主服务器选举。主服务器一旦下线，则重新触发选举，选举过程中阻塞，只有主服务器选举完成，才会执行其他任务。</li>
<li>某服务节点(引入elastic-job-lite-jar包)上线时会自动将服务器信息注册到注册中心，下线时会自动更新服务器状态。</li>
<li>主节点选举，服务器上下线，分片总数变更均更新重新分片标记。</li>
<li>定时任务触发时，如需重新分片，则通过主服务器分片，分片过程中阻塞，分片结束后才可执行任务。如分片过程中主服务器下线，则先选举主服务器，再分片。</li>
<li>通过上一项说明可知，为了维持作业运行时的稳定性，运行过程中只会标记分片状态，不会重新分片。分片仅可能发生在下次任务触发前。</li>
<li>每次分片都会按服务器IP排序，保证分片结果不会产生较大波动。</li>
<li>实现失效转移功能，在某台服务器执行完毕后主动抓取未分配的分片，并且在某台服务器下线后主动寻找可用的服务器执行任务</li>
</ul>
<h2 id="调度问题的复杂性"><a href="#调度问题的复杂性" class="headerlink" title="调度问题的复杂性"></a>调度问题的复杂性</h2><ul>
<li><p>弹性扩容<br>每个业务服务节点同时也是一个定时任务执行者, 各任务执行者之间不会直接通信，而是通过zookeeper协调, 定时任务状态存储在zk, 去中心化部署。<br>因此可以很方便地增加或者减少服务节点。</p>
</li>
<li><p>失效转移<br>如果在任务执行过程中有一个执行实例挂了，那么之前被分配到这个实例的任务（或者分片）会在下次任务执行之前被重新分配到其他正常节点实例上执行。</p>
</li>
<li><p>任务分片<br>elastic-job-lite并不直接提供数据处理的功能，框架只会将分片项分配至各个运行中的服务节点，开发者需要自行处理分片项与真实数据的对应关系。</p>
</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://codercheng.cn/2020/10/10/%E5%88%86%E5%B8%83%E5%BC%8F%E8%B0%83%E5%BA%A6/" data-id="cko3snvxa001ei8uo72flaj4j" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
</article>


  


  <nav id="page-nav">
    
    <a class="extend prev" rel="prev" href="/page/3/">&amp;laquo; 上一页</a><a class="page-number" href="/">1</a><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><span class="page-number current">4</span><a class="page-number" href="/page/5/">5</a><a class="page-number" href="/page/6/">6</a><a class="extend next" rel="next" href="/page/5/">下一页 &amp;raquo;</a>
  </nav>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/MySql/">MySql</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Netty/">Netty</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringBoot/">SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringCloud/">SpringCloud</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/SpringMVC/">SpringMVC</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Tomcat/">Tomcat</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Zookeeper/">Zookeeper</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E4%BD%9C%E4%B8%9A/">作业</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E5%88%86%E5%B8%83%E5%BC%8F%E8%B0%83%E5%BA%A6/">分布式调度</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E6%A1%86%E6%9E%B6/">框架</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E7%9F%A5%E8%AF%86/">知识</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a></li></ul>
    </div>
  </div>


  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2024/08/">八月 2024</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/09/">九月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/08/">八月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/04/">四月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">二月 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/11/">十一月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">六月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/02/">二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/01/">一月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/12/">十二月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">十一月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/10/">十月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/09/">九月 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">八月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2024/08/28/CentOS%E5%86%85%E5%AD%98cpu%E6%8E%92%E6%9F%A5/">CentOS内存cpu排查</a>
          </li>
        
          <li>
            <a href="/2022/09/22/Linux%20%E6%9F%A5%E7%9C%8B%E6%97%A5%E5%BF%97%E5%91%BD%E4%BB%A4/">Linux 查看日志命令</a>
          </li>
        
          <li>
            <a href="/2022/08/30/dubbo%20%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98/">dubbo 常见问题</a>
          </li>
        
          <li>
            <a href="/2022/04/26/1%20%E6%BA%90%E7%A0%81%E7%AC%94%E8%AE%B0/">1 源码笔记</a>
          </li>
        
          <li>
            <a href="/2022/02/24/jvisualVM%20%E4%BD%BF%E7%94%A8%E7%AC%94%E8%AE%B0/">jvisualVM 使用笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2024 CoderCheng<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//code.jquery.com/jquery-2.0.3.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>